#!/bin/bash

# config yum
cat > /etc/yum.repos.d/openEuler.repo << END_FILE
[mainline]
name=mainline
baseurl=http://119.3.219.20:82/openEuler:/Mainline/standard_aarch64
enabled=1
gpgcheck=0

[Factory]
name=Factory
baseurl=http://119.3.219.20:82/openEuler:/Factory/standard_aarch64
enabled=1
gpgcheck=0
END_FILE
yum makecache >/dev/null
if [ $? -eq 0 ];then
	echo "configure openEuler yum source succeed!"
else
	echo "configure openEuler yum source failed!"
	exit 1
fi

# check pkg install rely
cd /root/lao_wang;mkdir check_results;cd binaries
ls *.rpm | sort > rpm_pkglist
echo "checking pkg install rely is doing, please wait..."
for pkgname in `cat rpm_pkglist`
do
	yum localinstall -y $pkgname > install_res 2>&1
        if [ `cat install_res | grep "Complete!"` = "Complete!" ] &>/dev/null;then
 		echo "$pkgname is succeed!" >> ../check_results/succeed_install_pkglist
 		cat install_res >> ../check_results/succeed_log
        else
 		echo "$pkgname is failed!" >> ../check_results/failed_install_pkglist
		echo "yum localinstall -y $pkgname" >> ../check_results/failed_log
 		cat install_res >> ../check_results/failed_log
 		echo "=========================================================================" >> ../check_results/failed_log
 	fi
	rm -f install_res
done
echo "checking pkg install rely have done!"

# According to the failed_log file find which src rpmpkg provide it.
cd /root/lao_wang/check_results
if [ ! -f failed_log ];then
	echo "All packages were installed successfully!"
	exit 2
fi

rm -f /etc/yum.repos.d/openEuler.repo
cat > /etc/yum.repos.d/fc30.repo << END_FC
[fedora-30]
name=fedora-30
baseurl=http://mirrors.aliyun.com/fedora/releases/30/Everything/aarch64/os/
enabled=1
gpgcheck=0

[fedora-30-src]
name=fedora-30-src
baseurl=http://mirrors.aliyun.com/fedora/releases/30/Everything/source/tree/
enabled=1
gpgcheck=0

[fedora-30-u]
name=fedora-30-update
baseurl=http://mirrors.aliyun.com/fedora/updates/30/Everything/aarch64/
enabled=1
gpgcheck=0

[fedora-u-src]
name=fedora-u-src
baseurl=http://mirrors.aliyun.com/fedora/updates/30/Everything/SRPMS/
enabled=1
gpgcheck=0
END_FC
yum makecache >/dev/null
if [ $? -eq 0 ];then
	echo "configure fedora yum source succeed!"
else
	echo "configure fedora yum source failed!"
	exit 1
fi

echo "searching source pkgname is doing, please wait..."
cat failed_log | grep -E "localinstall|nothing provides" > nothing_provides_log_bak
sed -i 's/  - nothing provides //g' nothing_provides_log_bak
cat nothing_provides_log_bak | while read line
do
	echo "${line% needed*}" >> nothing_provides_log
done
rm -f nothing_provides_log_bak
cat  nothing_provides_log | while read name
do
	#echo "dnf repoquery --whatprovides "$name""
	if [[ "${name}" =~ "localinstall" ]] &>/dev/null;then
		echo "################################################################" >> nothing_provides_source_rpm
		echo "$name" >> nothing_provides_source_rpm
	else
		#echo "dnf repoquery --whatprovides "$name"" >> nothing_provides_source_rpm
		dnf repoquery --whatprovides "$name" &>/dev/null > res
		wpname=$(cat res)
		# dnf repoquery --whatprovides return null
		if [ -z $wpname ];then
			echo "Source       : $name" >> nothing_provides_source_rpm
		else
			#according binary pkgname to Find source pkgname
			dnf repoquery -i "${wpname%%-[0-9]*}" | grep Source | head -1 >> nothing_provides_source_rpm
		fi
	fi
	rm -f res
done
echo "searching source pkgname have done!"
